﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardLogistics.Model.Sys;
using System.Data.OracleClient;
using System.Data;
using System.Collections;

namespace CardLogistics.DAL.Sys
{
    public class Sys_SysRolesData
    {
        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(Sys_SysRoles model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vROLEID", OracleType.VarChar,50),
					new OracleParameter("vCODE", OracleType.VarChar,50),
					new OracleParameter("vFULLNAME", OracleType.VarChar,50),
					new OracleParameter("vDESCRIPTION", OracleType.VarChar,200),
					new OracleParameter("vENABLED", OracleType.Char,1),
					new OracleParameter("vSORTCODE", OracleType.Number),
					new OracleParameter("vDELETEMARK", OracleType.Char,1),
					new OracleParameter("vCREATEUSERID", OracleType.VarChar,50)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.CODE;
                parameters[2].Value = model.FULLNAME;
                parameters[3].Value = model.DESCRIPTION;
                parameters[4].Value = model.ENABLED;
                parameters[5].Value = model.SORTCODE;
                parameters[6].Value = model.DELETEMARK;
                parameters[7].Value = model.CREATEUSERID;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Sys_SysRoles_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(Sys_SysRoles model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vROLEID", OracleType.VarChar,50),
					new OracleParameter("vCODE", OracleType.VarChar,50),
					new OracleParameter("vFULLNAME", OracleType.VarChar,50),
					new OracleParameter("vDESCRIPTION", OracleType.VarChar,200),
					new OracleParameter("vENABLED", OracleType.Char,1),
					new OracleParameter("vSORTCODE", OracleType.Number),
					new OracleParameter("vDELETEMARK", OracleType.Char,1),
					new OracleParameter("vCREATEUSERID", OracleType.VarChar,50),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                parameters[0].Value = model.ROLEID;
                parameters[1].Value = model.CODE;
                parameters[2].Value = model.FULLNAME;
                parameters[3].Value = model.DESCRIPTION;
                parameters[4].Value = model.ENABLED;
                parameters[5].Value = model.SORTCODE;
                parameters[6].Value = model.DELETEMARK;
                parameters[7].Value = model.CREATEUSERID;
                parameters[8].Direction = ParameterDirection.Output;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Sys_SysRoles_Update", parameters, parameters[8]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,100),
					    new OracleParameter("vROLEID",  OracleType.VarChar)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Sys_SysRoles_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "Sys_SysRoles", StrFields, "ROLEID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ROLEID = '" + id + "' ", "*");
        }
        #endregion

        #region 角色授权

        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="rId">角色id</param>
        /// <param name="menus">权限集合</param>
        /// <returns></returns>
        public int RolesAddMenu(string rId, string[] menus)
        {
            ArrayList sqlList = new ArrayList();
            sqlList.Add("delete from SYS_SYSROLEMENUS where ROLEID='" + rId + "'");
            for (int i = 0; i < menus.Length; i++)
            {
                if (menus[i].Trim() != "")
                    sqlList.Add(" insert into  SYS_SYSROLEMENUS values('" + rId + "','" + menus[i] + "')");
            }
            return DbHelperSQL.ExecuteSqlTran(sqlList);
        }
        #endregion


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Sys_SysRoles GetModel(string rId)
        {
            Sys_SysRoles model = new Sys_SysRoles();
            DataRow row = GetInfoById(rId).Rows[0]; 
            if (row != null)
            {
                if (row["ROLEID"] != null)
                {
                    model.ROLEID = row["ROLEID"].ToString();
                }
                if (row["CODE"] != null)
                {
                    model.CODE = row["CODE"].ToString();
                }
                if (row["FULLNAME"] != null)
                {
                    model.FULLNAME = row["FULLNAME"].ToString();
                }
                if (row["DESCRIPTION"] != null)
                {
                    model.DESCRIPTION = row["DESCRIPTION"].ToString();
                }
                if (row["ENABLED"] != null)
                {
                    model.ENABLED = row["ENABLED"].ToString();
                }
                if (row["SORTCODE"] != null && row["SORTCODE"].ToString() != "")
                {
                    model.SORTCODE = int.Parse(row["SORTCODE"].ToString());
                }
                if (row["DELETEMARK"] != null)
                {
                    model.DELETEMARK = row["DELETEMARK"].ToString();
                }
                if (row["CREATEDATE"] != null && row["CREATEDATE"].ToString() != "")
                {
                    model.CREATEDATE = DateTime.Parse(row["CREATEDATE"].ToString());
                }
                if (row["CREATEUSERID"] != null)
                {
                    model.CREATEUSERID = row["CREATEUSERID"].ToString();
                }
            }
            return model;
        }

    }
}
